package com.wcity.manage.service.impl;

import com.alibaba.fastjson.JSON;
import com.wcity.manage.common.enums.Code;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.BlockMapper;
import com.wcity.manage.dao.mapper.PropertyVoiceMapper;
import com.wcity.manage.dao.mapper.RegisterMapper;
import com.wcity.manage.dao.model.Block;
import com.wcity.manage.dao.model.Register;
import com.wcity.manage.dao.model.Voice;
import com.wcity.manage.service.property.PropertyVoiceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 百姓心声管理
 * Created by zm on 2016/12/13.
 */
@Service
public class PropertyVoiceServiceImpl implements PropertyVoiceService {

    @Resource private PropertyVoiceMapper propertyVoiceMapper;
    @Resource private RegisterMapper registerMapper;
    @Resource private BlockMapper blockMapper;

    /**
     * 分页查询表单
     * @param pageModel
     * @return
     */
    @Override
    public List<Voice> getPayTypeListPage(PageModel pageModel) {
        return propertyVoiceMapper.findListPage(pageModel);
    }

    /**
     * 获取百姓心声
     * @param id
     * @return
     */
    @Override
    public Voice getVoiceInfo(String id) {
        return propertyVoiceMapper.getVoiceInfo( id );
    }

    /**
     * 回复
     * @param voice
     * @return
     */
    @Override
    public int voiceEdit(Voice voice) {
        return propertyVoiceMapper.update(voice);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public int replyDelete(String id) {
        return propertyVoiceMapper.delete(id);
    }

    /**
     * 查看回复内容
     * @param id
     * @param str
     * @return
     */
    @Override
    public Voice getVoiceInfo(String id, String str) {
        return propertyVoiceMapper.getReplyInfo(id,str);
    }

    /**
     * 修改状态
     * @param id
     * @param str
     * @return
     */
    @Override
    public int updateStatus(String id, String str) {
        return propertyVoiceMapper.updateStatus(id,str);
    }

    /**
     * 根据小区id获取百姓声音列表
     * @param pageModel
     * @return
     */
    @Override
    public Map<String, Object> getIVoiceList(PageModel pageModel) {
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> Map = new HashMap<>();
        List<Object> list = new ArrayList<>();
        try {
            List<Voice> voiceList = propertyVoiceMapper.findListPage(pageModel);
            System.out.println(voiceList.size());
            for ( Voice voice:voiceList ) {
                Register register = registerMapper.find( voice.getRegId()  );
                Map.put("icon",register.getIcon());
                Map.put("voice",voice);
                list.add(Map);
            }
            System.out.println(JSON.toJSONString(list));
            map.put("list",list);
            map.put("retCode", Code.SUCCESS.getCodeNum());
            map.put("retDesc","请求成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }

    /**
     * 发布百姓声音
     * @param content
     * @param images
     * @return
     */
    @Override
    public String voiceAdd( String content , String images , String blockId , String regId ) {

        try{
            Register reg =  registerMapper.find(Integer.parseInt(regId));
            Block block = blockMapper.find(Integer.parseInt(blockId));
            if(reg == null || block == null){
                return "用户不存在";
            }
            String nickname = reg.getNickname();
            String phone = reg.getPhone();
            int userId = block.getUserId();
            String blockName = block.getBlockName();
            Voice voice = new Voice();
            voice.setBlockId(Integer.parseInt( blockId ));
            voice.setCreateTime( new Date());
            voice.setUserId( userId );
            voice.setStatus( "未处理" );
            voice.setAccountNum( nickname );
            voice.setBlockName( blockName );
            voice.setPhone( phone );
            voice.setContent( content );
            voice.setImages( images );
            voice.setRegId( Integer.parseInt(regId) );

            int success = propertyVoiceMapper.insert( voice );

            if( success == 1) {
                return "发布成功";
            }
        }catch (Exception e ){
            e.printStackTrace();
            return "服务器异常";
        }
        return "";
    }

    /**
     * 获取我发布的声音
     * @param regId
     * @return
     */
    @Override
    public List<Voice> getIVoice(String regId) {
        return propertyVoiceMapper.getIVoice (regId);
    }

    /**
     * 获取声音详情
     * @param voiceId
     * @return
     */
    @Override
    public Voice getIVoiceInfo(String voiceId) {
        return propertyVoiceMapper.getIVoiceInfo (voiceId);
    }
}
